package Demo2_15;

import org.w3c.dom.Node;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-02-15
 * Time:15:23
 */
//自己实现队列
public class MyQueqe {
    public static class Node {
        public Node prev;
        public Node next;
        public int val;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node front;//队头
    public Node rear;//队尾

    public void offer(int val) {
        Node node = new Node(val);
        if (front == null) {
            front = node;
            rear = node;
        } else {
            rear.next = node;
            node.prev = rear;
            rear = node;
        }
    }

    public int poll() {
        if (front == null) {
            return -1;
        }
        int val = front.val;
        front = front.next;
        //防止只有一个节点
        if (front != null) {
            front.prev = null;
        } else {
            rear = null;
        }
        return val;
    }

    public int size() {
        Node cur = front;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public int peek() {
        if (front == null) {
            return -1;
        }
        return front.val;
    }

    public static void main(String[] args) {
        MyQueqe myQueqe = new MyQueqe();
        myQueqe.offer(12);
        myQueqe.offer(23);
        myQueqe.offer(34);
        myQueqe.offer(45);
        System.out.println(myQueqe.poll());
        System.out.println(myQueqe.peek());
    }
}
